package com.jiamao.goplay.data;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppApplication;
import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.ui.discover.SearchFragment;
import com.jiamao.goplay.util.GlideUtils;
import com.jiamao.lib.bean.ResponseBean;
import com.yuyh.library.imgsel.common.Constant;

import org.reactivestreams.Subscriber;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.utils.KLog;


public abstract class BaseSubscriber<T> extends DisposableObserver<T> {

    @Override
    public void onNext(T t) {

        BaseRep bean  = (BaseRep) t;
        KLog.d("HTML","BaseSubscriber  onNext bean: "+bean.getCode());
        if(bean.getCode() == Config.STATUS_INVALID || bean.getCode() == Config.STATUS_NOTLOGIN){
            KLog.d("HTML","STATUS_INVALID");
            AppApplication.getInstance().startLogins();
        }else if (bean.getCode() == Config.STATUS_TIPS_ADDR ){
            KLog.d("STATUS_TIPS_ADDR");
            showAddAddrDialog();
           // showAppDialog("每天最多可邀请100个用户");
        }else if (bean.getCode() == Config.STATUS_TIPS_YQ_MAX ){
            KLog.d("STATUS_TIPS_YQ_MAX");
            showAppDialog("每天最多可邀请100个用户");
        }

    }

    @Override
    public void onError(Throwable e) {
    //    LogUtil.d(e.getMessage());
    }

    private void showDialog(String msg){
        AlertDialog.Builder dialog = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        dialog.setMessage(msg);
        dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            //    MyApplication.app.startLogin();
            }
        });
        AlertDialog alertDialog = dialog.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();


    }


    private void showAppDialog(String msg){
        Activity activity = AppManager.getAppManager().currentActivity();
        if (activity == null){
            return;
        }

        View view = View.inflate(activity, R.layout.app_dialog_yqmax, null);
        TextView textView = view.findViewById(R.id.tv_msg);
        textView.setText(msg);
        final Button btnUpgrade = view.findViewById(R.id.btn_dialog_custom_upgrade);

        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setView(view)
                .create();
        btnUpgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialog.dismiss();
            }
        });

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                //   client.remove();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                // client.remove();
            }
        });
        dialog.show();


    }



    private void showAddAddrDialog(){
        Activity activity = AppManager.getAppManager().currentActivity();
        if (activity == null){
            return;
        }

        View view = View.inflate(activity, R.layout.app_dialog_tip_addr, null);

        final Button btnUpgrade = view.findViewById(R.id.btn_dialog_custom_upgrade);
        final Button clacen = view.findViewById(R.id.clacen);
        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setView(view)
                .create();
        clacen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btnUpgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Bundle bds = new Bundle();
               // bds.putString("uid",recent.getContactId());
                ComponentName componetName = new ComponentName("com.jiamao.goplay","com.jiamao.goplay.base.LibContainerActivity");
                Intent intent = new Intent();
                intent.putExtra("fragment", SearchFragment.class.getCanonicalName());
                intent.putExtra("bundle", bds);
                intent.setComponent(componetName);
                activity.startActivity(intent);
            }
        });
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                //   client.remove();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                // client.remove();
            }
        });
        dialog.show();


    }




}
